package study.lagou.com.mapper;

import org.apache.ibatis.annotations.*;
import study.lagou.com.pojo.Role;
import study.lagou.com.pojo.User;

import java.util.List;

public interface IUserMapper {

    /**
     * 添加用户
     * @param user
     */
    @Insert("insert into user values (#{id},#{username},#{password},#{birthday})")
    public void addUser(User user);

    /**
     * 修改用户
     * @param user
     */
    @Update("update user set username = #{username},password = #{password},birthday = #{birthday} where id = #{id}")
    public void updateUser(User user);

    /**
     * 查询用户
     */
    @Select("select * from user")
    public List<User> listUser();

    /**
     * 删除用户
     * @param id
     */
    @Delete("delete from user where id = #{id}")
    public void deleteUser(Integer id);

    /**
     * 通过id查询用户信息
     * @param id
     * @return
     */
    @Select("select * from user where id = #{id}")
    public User selectUserById(String id);

    /**
     * 查询用户并返回用户的订单信息
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "orderList",column = "id",
                    javaType = List.class,
                    many = @Many(select = "study.lagou.com.mapper.IOrderMapper.listOrderByUserId"))
    })
    public List<User> findUserAndOrder();

    /**
     * 查询用户和用户角色信息
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "roleList",column = "id",
                    javaType = List.class,
                    many = @Many(select = "study.lagou.com.mapper.IRoleMapper.listRoleByUserId"))
    })
    public List<User> findUserAndRole();
}
